<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="fbillno">
        <el-input
          v-model="queryParams.fbillno"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="fapproverid">
        <el-input
          v-model="queryParams.fapproverid"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核日期" prop="fapprovedate">
        <el-date-picker clearable
          v-model="queryParams.fapprovedate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="fmodifierid">
        <el-input
          v-model="queryParams.fmodifierid"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="fcreatedate">
        <el-date-picker clearable
          v-model="queryParams.fcreatedate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="fcreatorid">
        <el-input
          v-model="queryParams.fcreatorid"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改日期" prop="fmodifydate">
        <el-date-picker clearable
          v-model="queryParams.fmodifydate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="作废日期" prop="fcanceldate">
        <el-date-picker clearable
          v-model="queryParams.fcanceldate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择作废日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="作废人" prop="fcanceler">
        <el-input
          v-model="queryParams.fcanceler"
          placeholder="请输入作废人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据类型" prop="fbilltypeid">
        <el-input
          v-model="queryParams.fbilltypeid"
          placeholder="请输入单据类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期" prop="fdate">
        <el-date-picker clearable
          v-model="queryParams.fdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货方地址" prop="fTbibText1">
        <el-input
          v-model="queryParams.fTbibText1"
          placeholder="请输入收货方地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售部门" prop="fTbibBase1">
        <el-input
          v-model="queryParams.fTbibBase1"
          placeholder="请输入销售部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售员" prop="fTbibBase">
        <el-input
          v-model="queryParams.fTbibBase"
          placeholder="请输入销售员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:forecast:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:forecast:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:forecast:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:forecast:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="synchronization"
        >同步数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="forecastList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="预测单ID" align="center" prop="id" /> -->
      <el-table-column label="单据编号" align="center" prop="fBillNo" />
      <el-table-column label="单据状态" align="center" prop="fCancelStatus" />
      <el-table-column label="审核人" align="center" prop="fApproverId" />
      <el-table-column label="审核日期" align="center" prop="fApproveDate2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fApproveDate2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="fModifierId" />
      <el-table-column label="创建日期" align="center" prop="fCreateDate2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fCreateDate2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="fCreatorId" />
      <el-table-column label="修改日期" align="center" prop="fModifyDate2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fModifyDate2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作废日期" align="center" prop="fCancelDate2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fCancelDate2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作废人" align="center" prop="fCanceler" />
      <el-table-column label="作废状态" align="center" prop="fCancelStatus" />
      <el-table-column label="单据类型" align="center" prop="fBillTypeID" />
      <el-table-column label="日期" align="center" prop="fDate2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fDate2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货方地址" align="center" prop="f_TBIB_Text1" />
      <el-table-column label="销售部门" align="center" prop="f_TBIB_Base1" />
      <el-table-column label="销售员" align="center" prop="f_TBIB_Base" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:forecast:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:forecast:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预测单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="fbillno">
          <el-input v-model="form.fbillno" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="审核人" prop="fapproverid">
          <el-input v-model="form.fapproverid" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核日期" prop="fapprovedate">
          <el-date-picker clearable
            v-model="form.fapprovedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="fmodifierid">
          <el-input v-model="form.fmodifierid" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="创建日期" prop="fcreatedate">
          <el-date-picker clearable
            v-model="form.fcreatedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="fcreatorid">
          <el-input v-model="form.fcreatorid" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="修改日期" prop="fmodifydate">
          <el-date-picker clearable
            v-model="form.fmodifydate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作废日期" prop="fcanceldate">
          <el-date-picker clearable
            v-model="form.fcanceldate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择作废日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作废人" prop="fcanceler">
          <el-input v-model="form.fcanceler" placeholder="请输入作废人" />
        </el-form-item>
        <el-form-item label="单据类型" prop="fbilltypeid">
          <el-input v-model="form.fbilltypeid" placeholder="请输入单据类型" />
        </el-form-item>
        <el-form-item label="日期" prop="fdate">
          <el-date-picker clearable
            v-model="form.fdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货方地址" prop="fTbibText1">
          <el-input v-model="form.fTbibText1" placeholder="请输入收货方地址" />
        </el-form-item>
        <el-form-item label="销售部门" prop="fTbibBase1">
          <el-input v-model="form.fTbibBase1" placeholder="请输入销售部门" />
        </el-form-item>
        <el-form-item label="销售员" prop="fTbibBase">
          <el-input v-model="form.fTbibBase" placeholder="请输入销售员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listForecast, getForecast, delForecast, addForecast, updateForecast,syncData } from "@/api/system/forecast";

export default {
  name: "Forecast",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预测单表格数据
      forecastList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fbillno: null,
        fdocumentstatus: null,
        fapproverid: null,
        fapprovedate: null,
        fmodifierid: null,
        fcreatedate: null,
        fcreatorid: null,
        fmodifydate: null,
        fcanceldate: null,
        fcanceler: null,
        fcancelstatus: null,
        fbilltypeid: null,
        fdate: null,
        fTbibText1: null,
        fTbibBase1: null,
        fTbibBase: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    synchronization(){
      syncData().then(res => {
        this.$message.success('同步数据成功!')
      })
    },
    /** 查询预测单列表 */
    getList() {
      this.loading = true;
      listForecast(this.queryParams).then(response => {
        this.forecastList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fbillno: null,
        fdocumentstatus: null,
        fapproverid: null,
        fapprovedate: null,
        fmodifierid: null,
        fcreatedate: null,
        fcreatorid: null,
        fmodifydate: null,
        fcanceldate: null,
        fcanceler: null,
        fcancelstatus: null,
        fbilltypeid: null,
        fdate: null,
        fTbibText1: null,
        fTbibBase1: null,
        fTbibBase: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预测单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getForecast(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预测单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateForecast(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForecast(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预测单编号为"' + ids + '"的数据项？').then(function() {
        return delForecast(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/forecast/export', {
        ...this.queryParams
      }, `forecast_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
